package Project1;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class EmployeeDriver {
	public static void main(String [] args){
		Class klasses [] = {Employee.class, DepartmentEmp.class, DepartmentManager.class, ContactInfo.class,
		       Salaries.class, Titles.class, Department.class, Projects.class};
		HibernateContext.addClasses(klasses);
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String command;
        do{
        	System.out.print(">");
        	try{command = stdin.readLine();}
        	catch(IOException ex){
        		command = "?";
        		System.out.println("\nImproper input");}
        	String split [] = command.split(" ");
        	if(split[0].equalsIgnoreCase("find") && split.length >1){
        		if (split[1].equalsIgnoreCase("employee") && split.length == 4){
        			if(split[2].equalsIgnoreCase("Projects")){
        				long num = Long.parseLong(split[3]);
            			Employee e = Employee.find(num);
            			String projs = "\nProjects for " +e.getFirst_name() + " " + e.getLast_name() ;
            			for(Projects p : e.getProjects()){
            				projs+="\n\t" +p.getProj_name();
            			}
            			System.out.println(projs);
        			}
        			else if(split[2].equalsIgnoreCase("salaries")){
        				long num = Long.parseLong(split[3]);
            			Employee e = Employee.find(num);
            			String sal= "\nPast salaries for " +e.getFirst_name() + " " + e.getLast_name();
            			for(Salaries s: e.getSalaries()){
            				s.print();
            			}
            			System.out.println(sal);
        			}
        			else{
        				List <Employee> e = Employee.find(split[2], split[3]);
        				for(Employee emp : e){
        					emp.print();
        				}
        			}
        		}
        		else if (split[1].equalsIgnoreCase("employee") && split.length ==3){
        			
        				Long num = Long.parseLong(split[2]);
        				Employee.find(num).print();
        		}
        		else if(split[1].equalsIgnoreCase("Departments")){
        			if (split.length == 4 && split[2].equalsIgnoreCase("employee")){
        				Department d = Department.findByNum(split[3]);
        				DepartmentEmp.employeesOf(d.getDept_name());
        			}
        			else
        				Department.list();

            	}
        		else if (split[1].equalsIgnoreCase("managers")){
        			Employee.printManagers();
        		}
        		else if(split[1].equalsIgnoreCase("Projects") ){
        			if (split.length ==3){
        				int num = Integer.parseInt(split[2]);
        				Projects p = Projects.find(num);
        				if (p!=null){
        					String eName = "All Employees on " + p.getProj_name()  + " project";
        					for(Employee e : p.getEmployees()){
        						eName+= "\n\t"+ e.getFirst_name()+" " + e.getLast_name();
        					}
        					System.out.println(eName);
        				}
        			}
        			else {
        				Projects.list();
        			}
            	}
        	}
        		
        
        	else if(split[0].equalsIgnoreCase("add") && split.length == 4){
        		if (split[1].equalsIgnoreCase("contact")){
        			Long num = Long.parseLong(split[2]);
        			try{
        				Employee.find(num).addContactInfo(split[3]);
        			}
        			catch(Exception e){
        				System.out.println("Contact info already exists for this employee");
        			}
        		}
        		else if (split[1].equalsIgnoreCase("project")){
        			Long num = Long.parseLong(split[2]);
        			int proj = Integer.parseInt(split[3]);
        			Employee.find(num).addProject(proj);
        		}
        	}
        	
        	
        	else if (split[0].equalsIgnoreCase("new") && split.length==4){
        		if(split[1].equalsIgnoreCase("project")){
        			int num = Integer.parseInt(split[2]);
        			Projects.add(num, split[3]);
        		}
        	}
        }
        while(!command.equalsIgnoreCase("quit"));
	}
}
